public class AverageExceptMaxMin implements Averagable<Double>
{
    public double average(Double[] value,int n)
    {
        double sum=0, min=0, max=0;
        if(n<3) throw new UnsupportedAverageException("n is "+n+" but the minimum should be 3");
        if(n>value.length) throw new UnsupportedAverageException("n is "+n+" but the maximum should be "+value.length);
        max=min=sum=value[0];
        for(int i=1;i<n;++i)
        {
            max=Math.max(max,value[i]);
            min=Math.min(min,value[i]);
            sum+=value[i];
        }
        sum=(sum-max-min)/(n-2);
        return sum;
    }

    public double average(Double[] value)
    {
        return average(value,value.length);
    }

    public double average(String[] value,int n)
    {
        if(n<3) throw new UnsupportedAverageException("n is "+n+" but the minimum should be 3");
        if(n>value.length) throw new UnsupportedAverageException("n is "+n+" but the maximum should be "+value.length);
        Double v[]=new Double[n];
        for(int i=0;i<n;++i)
            try
            {
                v[i]=Double.parseDouble(value[i]);
            }catch(NumberFormatException ex)
            {
                throw new MyNumberFormatException("value["+i+"] "+value[i]);
            }
        return average(v,n);
    }

    public double average(String[] value)
    {
        return average(value,value.length);
    }
}
